﻿
// ———————————————————————–
// <copyright file="NiemCadTests.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// NiemCadTests.cs:
//
// Broad tests for the NIEMCAD library.
//
// Project: NIEMCADTest
// Author:  Lizzie DeYoung - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)  
      

using EDXLSharp.NIEMCADLib;
using EDXLSharp.NIEMCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using EDXLSharp;

namespace NIEMCADTest
{
  [TestClass]
  public class NiemCadTests
  {
    [TestMethod]
    public void CadIncidentConstructedFromXml()
    {
      try
      {
        CADEvent ce = new CADEvent(TestData.CadIncidentXml);
      }
      catch (Exception ex)
      {
        Debug.Write(ex);
        Assert.Fail("Failed to construct a CAD Incident from XML", ex);
      }
    }

    [TestMethod]
    public void CadResourceConstructedFromXml()
    {
      try
      {
        CADEvent ce = new CADEvent(TestData.CadResourceXml);
      }
      catch (Exception ex)
      {
        Debug.Write(ex);
        Assert.Fail("Failed to construct a CAD Resource from XML", ex);
      }
    }

    [TestMethod]
    public void WriteCadResourceToXml()
    {
      CADEvent ce = new CADEvent();
      ce.EventID = Guid.NewGuid().ToString();
      ce.EventTypeDescriptor = new EventTypeDescriptor();
      ce.EventTypeDescriptor.TypeCode = new EventTypeCode();
      ce.EventTypeDescriptor.TypeCode.Code = EventTypeCodeHelper.GetCode("ATOM.GRDTRK.EQT.GRDVEH.CVLVEH.EM.EMS");
      ce.EventLocation = new EventLocation();
      LocationCylinder lc = new LocationCylinder();
      lc.LocationCylinderHalfHeight = 5;
      lc.LocationCylinderRadius = 10;
      //TODO: LDY this initialization should be done in the constructor of locationcylinder.
      lc.LocationPoint = new LocationPoint();
      lc.LocationPoint.Point = new GeoOASISWhereLib.GMLPoint(EDXLSharp.EDXLConstants.GML32Namespace);
      //TODO: LDY Point.Id should be validated or auto-populated. the schema throws an error if ti's not filled.
      lc.LocationPoint.Point.ID = "asfasdfasdfasdf";
      //TODO: LDY Point.SrsName should be autopopulated to the value below.
      lc.LocationPoint.Point.SrsName = new Uri("http://metadata.ces.mil/mdr/ns/GSIP/crs/WGS84E_3D");
      lc.LocationPoint.Point.Pos.Pos.Add(22);
      lc.LocationPoint.Point.Pos.Pos.Add(31);
      lc.LocationPoint.Point.Pos.Pos.Add(44);
      ce.EventLocation.LocationGeospatialCoordinate = lc;
      ce.ValidityRange = new EventValidityDateTimeRange(DateTime.Now, DateTime.Now.AddHours(1));
      ce.MessageDate = DateTime.Now;

      ResourceDetail res = new ResourceDetail();
      res.Status = new ResourceStatus();
      res.Status.ResourcePrimaryStatus = new ResourcePrimaryStatus();
      res.Status.SecondaryStatuses.Add(new ResourceEIDDStatus());
      res.OwningOrganization = new ResourceOwningOrganization();
      res.OwningOrganization.OrganizationIdentification = new OrganizationIdentification("lafd.la.us");
      res.OwningOrganization.ResourceIdentifier = new ResourceIdentifier("1234");
      res.ControllingOrganization = new ResourceControllingOrganization();
      res.ControllingOrganization.OrganizationIdentification = new OrganizationIdentification("laco.la.us");
      res.ControllingOrganization.ResourceIdentifier = new ResourceIdentifier("4321");

      ce.Detail = res;

      string str = ce.ToString();
      Debug.Write(str);

      System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(str);
      System.Xml.Linq.XElement xelt = xdoc.Root;
    }

    [TestMethod]
    public void WriteCadIncidentToXml()
    {
      CADEvent ce = new CADEvent();
      ce.EventID = Guid.NewGuid().ToString();
      ce.EventTypeDescriptor = new EventTypeDescriptor();
      ce.EventTypeDescriptor.TypeCode = new EventTypeCode();
      ce.EventTypeDescriptor.TypeCode.Code = EventTypeCodeHelper.GetCode("BITS.REPORT.EM.DISPATCH.MEDICAL.TRAUMA.MVA.MULTI");
      ce.EventLocation = new EventLocation();
      LocationCylinder lc = new LocationCylinder();
      lc.LocationCylinderHalfHeight = 5;
      lc.LocationCylinderRadius = 10;
      //TODO: LDY this initialization should be done in the constructor of locationcylinder.
      lc.LocationPoint = new LocationPoint();
      lc.LocationPoint.Point = new GeoOASISWhereLib.GMLPoint(EDXLSharp.EDXLConstants.GML32Namespace);
      //TODO: LDY Point.Id should be validated or auto-populated. the schema throws an error if ti's not filled.
      lc.LocationPoint.Point.ID = "asfasdfasdfasdf";
      //TODO: LDY Point.SrsName should be autopopulated to the value below.
      lc.LocationPoint.Point.SrsName = new Uri("http://metadata.ces.mil/mdr/ns/GSIP/crs/WGS84E_3D");
      lc.LocationPoint.Point.Pos.Pos.Add(22);
      lc.LocationPoint.Point.Pos.Pos.Add(31);
      lc.LocationPoint.Point.Pos.Pos.Add(44);
      ce.EventLocation.LocationGeospatialCoordinate = lc;
      ce.ValidityRange = new EventValidityDateTimeRange(DateTime.Now, DateTime.Now.AddHours(1));
      ce.MessageDate = DateTime.Now;

      IncidentDetail inc = new IncidentDetail();
      inc.Status = new IncidentStatus();
      inc.Status.IncidentPrimaryStatus = new IncidentPrimaryStatus();
      inc.OwningOrganization = new IncidentOwningOrganization();
      inc.OwningOrganization.OrganizationIdentification = new OrganizationIdentification("lafd.la.us");
      inc.OwningOrganization.IncidentIdentifier = new IncidentIdentifier("1234");

      ce.Detail = inc;

      string str = ce.ToString();
      Debug.Write(str);

      System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(str);
      System.Xml.Linq.XElement xelt = xdoc.Root;
    }


    [TestMethod]
    public void DateTimeFormat()
    {
      CADEvent ce = new CADEvent();
      ce.EventID = Guid.NewGuid().ToString();
      ce.EventTypeDescriptor = new EventTypeDescriptor();
      ce.EventTypeDescriptor.TypeCode = new EventTypeCode();
      ce.EventTypeDescriptor.TypeCode.Code = EventTypeCodeHelper.GetCode("BITS.REPORT.EM.DISPATCH.MEDICAL.TRAUMA.MVA.MULTI");
      ce.EventLocation = new EventLocation();
      LocationCylinder lc = new LocationCylinder();
      lc.LocationCylinderHalfHeight = 5;
      lc.LocationCylinderRadius = 10;
      //TODO: LDY this initialization should be done in the constructor of locationcylinder.
      lc.LocationPoint = new LocationPoint();
      lc.LocationPoint.Point = new GeoOASISWhereLib.GMLPoint(EDXLSharp.EDXLConstants.GML32Namespace);
      //TODO: LDY Point.Id should be validated or auto-populated. the schema throws an error if ti's not filled.
      lc.LocationPoint.Point.ID = "asfasdfasdfasdf";
      //TODO: LDY Point.SrsName should be autopopulated to the value below.
      lc.LocationPoint.Point.SrsName = new Uri("http://metadata.ces.mil/mdr/ns/GSIP/crs/WGS84E_3D");
      lc.LocationPoint.Point.Pos.Pos.Add(22);
      lc.LocationPoint.Point.Pos.Pos.Add(31);
      lc.LocationPoint.Point.Pos.Pos.Add(44);
      ce.EventLocation.LocationGeospatialCoordinate = lc;
      ce.ValidityRange = new EventValidityDateTimeRange(DateTime.Now, DateTime.Now.AddHours(1));
      ce.MessageDate = DateTime.Now;

      IncidentDetail inc = new IncidentDetail();
      inc.Status = new IncidentStatus();
      inc.Status.IncidentPrimaryStatus = new IncidentPrimaryStatus();
      inc.OwningOrganization = new IncidentOwningOrganization();
      inc.OwningOrganization.OrganizationIdentification = new OrganizationIdentification();
      inc.OwningOrganization.OrganizationIdentification.ID = "lafd.la.us";
      inc.OwningOrganization.IncidentIdentifier = new IncidentIdentifier();
      inc.OwningOrganization.IncidentIdentifier.ID = "1234";

      ce.Detail = inc;

      string str = ce.ToString();
      Debug.Write(str);

      XDocument xdoc = XDocument.Parse(str);
      XElement evt = xdoc.Root;
      XElement range = evt.Element(XName.Get("EventValidityDateTimeRange", EDXLConstants.MilopsNamespace));
      XElement start = range.Element(XName.Get("StartDate", EDXLConstants.NiemCoreNamespace)).Element(XName.Get("DateTime", EDXLConstants.NiemCoreNamespace));
      XElement end = range.Element(XName.Get("EndDate", EDXLConstants.NiemCoreNamespace)).Element(XName.Get("DateTime", EDXLConstants.NiemCoreNamespace));
      XElement msg = evt.Element(XName.Get("EventMessageDateTime", EDXLConstants.MilopsNamespace)).Element(XName.Get("DateTime", EDXLConstants.NiemCoreNamespace));

      //2014-08-11T08:31:41.00Z
      string timePattern = @"^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\dZ";

      Assert.IsTrue(Regex.IsMatch(start.Value, timePattern), "Validity start time is not in the correct format");
      Assert.IsTrue(Regex.IsMatch(end.Value, timePattern), "Validity end time is not in the correct format");
      Assert.IsTrue(Regex.IsMatch(msg.Value, timePattern), "Message time is not in the correct format");

    }
  }
}